package com.zhoushihao.class0821;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class AttendanceSystem {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Set<String> employeeName = new HashSet<>();
        employeeName.add("赵本山");
        employeeName.add("李连杰");
        employeeName.add("马云");
        Map<String, LocalDateTime> checkInMap = new HashMap<>();
        Map<String, LocalDateTime> checkOutMap = new HashMap<>();
        Set<String> currentDateAttendanceRecords = new HashSet<>();
        Map<LocalDate, Set<String>> AttendanceTable = new HashMap<>();
        LocalDate today = LocalDate.now();
        while (true) {
            System.out.println("员工考勤系统");
            System.out.println("0----退出");
            System.out.println("1----签到");
            System.out.println("2----签退");
            System.out.println("3----显示考勤信息");
            System.out.println("4----显示全部员工");

            int n = sc.nextInt();
            sc.nextLine();
            if (n == 0) {
                break;

            } else if (n == 1) {
                System.out.println("请输入要签到的员工姓名");
                String name = sc.nextLine();
                if (employeeName.contains(name)) {
                    if (checkInMap.containsKey(name)) {
                        System.out.println("已经签到过了");
                    } else {
                        checkInMap.put(name, LocalDateTime.now());
                        currentDateAttendanceRecords.add(name);
                        AttendanceTable.put(today, currentDateAttendanceRecords);
                        System.out.println(name + "签到成功");
                    }
                } else {
                    System.out.println("不存在该员工");
                }
            } else if (n == 2) {
                System.out.println("请输入要签退的员工姓名");
                String name = sc.nextLine();
                if (employeeName.contains(name)) {
                    if (checkInMap.containsKey(name)) {
                        checkOutMap.put(name, LocalDateTime.now());
                        System.out.println(name + "签退成功");
                    } else {
                        System.out.println("你没有签到，不能签退");
                    }
                } else {
                    System.out.println("不存在该员工");
                }
            } else if (n == 3) {
                System.out.println("请输入要查找的日期（yyyyMMdd），今天直接回车");
                String date = sc.nextLine();
                if (date.isEmpty()) {
                    if (AttendanceTable.containsKey(today)) {
                        PrintRecords(checkInMap, checkOutMap, AttendanceTable, today);
                    } else {
                        System.out.println("没有员工考勤记录");
                    }
                } else {
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
                    LocalDate searchDate = LocalDate.parse(date, formatter);
                    if (AttendanceTable.containsKey(searchDate)) {
                        PrintRecords(checkInMap, checkOutMap, AttendanceTable, searchDate);
                    } else {
                        System.out.println("没有员工考勤记录");
                    }
                }
            } else if (n == 4) {
                for (String name : employeeName) {
                    System.out.println(name);
                }
            }
        }

    }

    public static void PrintRecords(Map<String, LocalDateTime> checkInMap, Map<String, LocalDateTime> checkOutMap, Map<LocalDate, Set<String>> attendanceTable, LocalDate searchDate) {
        Set<String> nameSet = attendanceTable.get(searchDate);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        for (String name : nameSet) {
            String checkInTime = checkInMap.get(name).format(formatter);
            String checkOutTime = "";
            if (checkOutMap.containsKey(name)) {
                checkOutTime = checkOutMap.get(name).format(formatter);
            }
            System.out.println(name + "签到时间为" + checkInTime + "," + "签退时间为" + checkOutTime);
        }
    }
}
